package com.ja.openchat.ui.contact;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentGroupListBinding;
import com.ja.openchat.ui.base.BaseFragmentPagerAdapter;
import com.ja.openchat.ui.contact.search.PublicSearchFragment;
import com.ja.openchat.viewmodels.GroupListViewModel;
import com.zy.openchat.core.model.contact.Section;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

public class GroupListFragment extends BaseFragment <FragmentGroupListBinding, GroupListViewModel> {

    public GroupListAdapter groupListAdapter;
    private List<Section> sectionList = new ArrayList<>();
    private int type;


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_group_list;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initParam() {
        super.initParam();
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("type");
        }
    }

    protected List<String> pagerTitleString() {
        List<String> list = new ArrayList<>();
        list.add(getString(R.string.droups_i_manage));
        list.add(getString(R.string.groups_i_joined));
        return list;
    }

    protected List<Fragment> pagerFragment() {
        List<Fragment> list = new ArrayList<>();
        list.add(GroupListChildFragment.newInstance(0));
        list.add(GroupListChildFragment.newInstance(1));
        return list;
    }

    @Override
    public void initView() {
        super.initView();
        viewModel.setFragment(this);
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), pagerFragment(), pagerTitleString());
        binding.viewPager.setAdapter(pagerAdapter);
        binding.tabs.setupWithViewPager(binding.viewPager);
        binding.viewPager.setCurrentItem(type);
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                GroupListChildFragment fragment = (GroupListChildFragment) pagerFragment().get(binding.viewPager.getCurrentItem());
//                fragment.reloadGroups();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        binding.header.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startContainerActivity(PublicSearchFragment.class.getCanonicalName());
//            }
//        });

        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

}
